
var arrLinkPage=[""];
var flag = 0;
var pageNum = 0;
var page = 0;
var currentPage = 0;
var firstPage = "";

function recentposts(json) {
	
	var linkCurrent = location.href;
	var totalposts = json.feed.openSearch$totalResults.$t;
		
	var m=location.href;
	var	l=m.indexOf("/search/label/")!=-1;
	var a=l?m.substr(m.indexOf("/search/label/")+14,m.length):"";		
	a=a.indexOf("?")!=-1?a.substr(0,a.indexOf("?")):a;
	var labelStatus = l;
	var labelContent = a;	

	var endPage = Math.ceil(totalposts/10);
	
	if (endPage == 0) { retrun;	}
	
	//------------------------------
	if (pageNum == 0) {
		var tmp01 = Math.floor(totalposts/500);
		//document.write('+++++++++++++++++' + tmp01);
		if (tmp01 != 0) 
		{
			for (var i=1; i<=tmp01;i++) {
				if (labelStatus == true) {
					document.write('<script src="http://blog.vnlives.net/feeds/posts/summary/-/' + labelContent + '?alt=json-in-script&callback=recentposts&max-results=99999&start-index=' + i*50+1 + '" type="text/javascript"><\/script>');
				} else {
					document.write('<script src="http://blog.vnlives.net/feeds/posts/summary?alt=json-in-script&callback=recentposts&max-results=99999&start-index=' + i*50+1 + '" type="text/javascript"><\/script>');
				}
			}
		}
	}
	//------------------------------

	var tmpLink;
	var postspublished;
	
	var linkPage;
	
	for (var i = 0; i < json.feed.entry.length; i++)
	{	
		//document.getElementById('paging').innerHTML += " -- " + i;
		tmpLink = "";
		postspublished = json.feed.entry[i].published.$t;
		
		postspublished = postspublished.substring(0,19)+postspublished.substring(23,29);
		postspublished = encodeURIComponent(postspublished);
		
		if (pageNum == 0) {
			if (labelStatus == true) {
				tmpLink = "/search/label/"+labelContent+"?max-results=10";
			} else {
				tmpLink = "/search/?max-results=10";
			}
			firstPage = "http://blog.vnlives.net" + tmpLink;
		} else {
			if (labelStatus == true) {
				tmpLink = "/search/label/"+labelContent+"?updated-max="+ postspublished +"&max-results=10";
			} else {
				tmpLink = "/search?updated-max=" + postspublished + "&max-results=10";
			}		
		}

		if (pageNum == 499) {
			tmpLink = tmpLink + "&reverse-paginate=true";
		}
		
		if (pageNum >= 500) {
			tmpLink = tmpLink + "&reverse-paginate=true&start=10&by-date=false";
		}		
		
		if (pageNum == 0) 
		{
			page++;

			if (linkCurrent == "http://blog.vnlives.net" + tmpLink) {
				currentPage = page;
				arrLinkPage.push("<span class='current'>" + page + "</span>");
			} 
			else 
			{
				arrLinkPage.push("<a href='http://blog.vnlives.net" + tmpLink + "'>" + page + "</a>");
			}

		} 
		else if ((pageNum+1)%10 == 0) 
		{
			page++;

			if (linkCurrent == "http://blog.vnlives.net" + tmpLink) {
				currentPage = page;
				arrLinkPage.push("<span class='current'>" + page + "</a></span>");
			}	
			else 
			{
				arrLinkPage.push("<a href='http://blog.vnlives.net" + tmpLink + "'>" + page + "</a>");
			}
		
		}
		
		
		var i;
		if (currentPage < 5 && currentPage != 0) 
		{
			
			var textContent = document.getElementById('paging').innerHTML;
				
			if (textContent.indexOf("<a href=\"http://") == -1) 
			{
			
				//document.write('--- 1:'+ pageNum + ' 2:' + endPage + ' 3:' + totalposts + ' 4:' + arrLinkPage.length + ' 5:' + currentPage);	
				if (page == endPage && endPage <= 9) 
				{
				
					var b='<span class="pages">Page ' + currentPage +' of ' + endPage + "</span> ";
					b+='<a href="'+firstPage+'">'+pageNaviConf.firstText+"</a>"
					document.getElementById('paging').innerHTML += b;	
				
					for (i = 0; i < arrLinkPage.length; i++) 
					{
						document.getElementById('paging').innerHTML += arrLinkPage[i];
					}
					break;
				
				}
				
				if (pageNum > (currentPage+7)*10) 
				{
				
					var b='<span class="pages">Page ' + currentPage +' of ' + endPage + "</span> ";
					b+='<a href="'+firstPage+'">'+pageNaviConf.firstText+"</a>"
					document.getElementById('paging').innerHTML += b;	

					for (i = 0; i < arrLinkPage.length-currentPage+1; i++) 
					{
						document.getElementById('paging').innerHTML += arrLinkPage[i];
					}
					break;
				}
			}
		
		}
		
		if (currentPage >= 5) 
		{
			
			var textContent = document.getElementById('paging').innerHTML;
				
			if (textContent.indexOf("<a href=\"http://") == -1) 
			{

				//document.write('+++++++++++++++++'+ pageNum + ' ---- ' + endPage + ' --- ' + totalposts + ' --- ' + arrLinkPage.length);	
				if (page == endPage && endPage <= 9) 
				{
				
					var b='<span class="pages">Page ' + currentPage +' of ' + endPage + "</span> ";
					b+='<a href="'+firstPage+'">'+pageNaviConf.firstText+"</a>"
					document.getElementById('paging').innerHTML += b;					
				
					for (i = 0; i < arrLinkPage.length; i++) 
					{
						document.getElementById('paging').innerHTML += arrLinkPage[i];
					}
					break;
				
				}
				
				if (pageNum > (currentPage+3)*10)
				{
				
					var b='<span class="pages">Page ' + currentPage +' of ' + endPage + "</span> ";
					b+='<a href="'+firstPage+'">'+pageNaviConf.firstText+"</a>"
					document.getElementById('paging').innerHTML += b;	
				
					//document.getElementById('paging').innerHTML += textContent;
					for (i = currentPage-4; i < arrLinkPage.length; i++) 
					{
						document.getElementById('paging').innerHTML += arrLinkPage[i];
					}					
					break;
				}
				
				//document.getElementById('paging').innerHTML += " ++ " + pageNum + " -- " + totalposts;
				if (currentPage+3 >= endPage && pageNum == totalposts-1) {
				
					var b='<span class="pages">Page ' + currentPage +' of ' + endPage + "</span> ";
					b+='<a href="'+firstPage+'">'+pageNaviConf.firstText+"</a>"
					document.getElementById('paging').innerHTML += b;	
				
					//document.getElementById('paging').innerHTML += textContent;
					for (i = endPage-8; i < arrLinkPage.length; i++) 
					{
						document.getElementById('paging').innerHTML += arrLinkPage[i];
					}					
					break;				
				
				}
				
			}			
		
		}		
			
		pageNum++;
	}
		
}
	
var m=location.href;
var	l=m.indexOf("/search/label/")!=-1;
var a=l?m.substr(m.indexOf("/search/label/")+14,m.length):"";		
a=a.indexOf("?")!=-1?a.substr(0,a.indexOf("?")):a;
var labelStatus = l;
var labelContent = a;

if (labelStatus == true) {

	document.write('<script src="http://blog.vnlives.net/feeds/posts/summary/-/' + labelContent + '?alt=json-in-script&callback=recentposts&max-results=99999" type="text/javascript"><\/script>');
	//document.write('<script src="http://blog.vnlives.net/feeds/posts/summary/-/' + labelContent + '?alt=json-in-script&callback=recentposts&max-results=99999&start-index=501" type="text/javascript"><\/script>');
	//document.write('<script src="http://blog.vnlives.net/feeds/posts/summary/-/' + labelContent + '?alt=json-in-script&callback=recentposts&max-results=99999&start-index=1001" type="text/javascript"></\script>');

} else {

	document.write('<script src="http://blog.vnlives.net/feeds/posts/summary?alt=json-in-script&callback=recentposts&max-results=99999" type="text/javascript"><\/script>');
	//document.write('<script src="http://blog.vnlives.net/feeds/posts/summary?alt=json-in-script&callback=recentposts&max-results=99999&start-index=501" type="text/javascript"><\/script>');
	//document.write('<script src="http://blog.vnlives.net/feeds/posts/summary?alt=json-in-script&callback=recentposts&max-results=99999&start-index=1001" type="text/javascript"><\/script>');
}